﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using FOBVT.Web.MainClass;
using FOBVT.Web.DBUtility;
using FOBVT.Web.DBUtility.ManagerImpl;

namespace FOBVT.Web.Admin
{
	public partial class ListNews : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!base.IsPostBack)
			{
				this.BindData();
			}
		}

		//NewsDaManagerImpl impl = new NewsDaManagerImpl();
		public void BindData()
		{
			PagingEntity pe = new PagingEntity("News", "PublishDate desc");
			pe.PageSize = Pagination.PageSize;
			pe.PageIndex = Pagination.PageIndex;
			DataTable table = YOCOSBusiness.NewsManager.GetNewsListPagin(pe);
			this.rptNewsList.DataSource = table;
			this.rptNewsList.DataBind();
			this.Pagination.BindName = "BindData";
			this.Pagination.Count = pe.RecordCount;
			this.Pagination.BindPager();
		}

		protected string GetShortNewsContent(string content)
		{
			return StringUtil.GetShortContent(content, 40);
		}

		protected void DeleteNewsByLink(object sender, EventArgs e)
		{
			string Id = ((LinkButton)sender).CommandArgument.ToString();
			bool hassuccess = YOCOSBusiness.NewsManager.DeleteNewsByIds(Id);
			if (hassuccess)
			{
				this.BindData();
				FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Delete successfully!!");
			}
			else
			{
				FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Delete Failed!!");
			}
		}
		protected void BenDeleteSelect(object sender, EventArgs e)
		{
			string ID = string.Empty;
			foreach (RepeaterItem item in this.rptNewsList.Items)
			{
				CheckBox CheckboxSign = item.FindControl("ChkSelect") as CheckBox;
				if (CheckboxSign.Checked == true)
				{
					if (ID == string.Empty)
					{
						ID = ((HiddenField)this.rptNewsList.Items[item.ItemIndex].FindControl("HiddenId")).Value;
					}
					else
					{
						ID += "," + ((HiddenField)this.rptNewsList.Items[item.ItemIndex].FindControl("HiddenId")).Value;
					}

				}
			}
			if (ID != string.Empty)
			{
				bool hassuccess = YOCOSBusiness.NewsManager.DeleteNews(Convert.ToInt64(ID));
				if (hassuccess)
				{
					this.BindData();
					FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Delete successfully!!");
				}
				else
				{
					FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Delete Failed!!");
				}
			}
			else
			{
				FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Please select item to delete!!");
			}
		}

		protected void BtnSearchRecord_Click(object sender, EventArgs e)
		{
			string startDate = this.TxbStartDate.Text;
			string endDate = this.TxbEndDate.Text;
			startDate = DateTime.Parse(startDate).ToString("yyyy/MM/dd 00:00:00");
			endDate = DateTime.Parse(endDate).ToString("yyyy/MM/dd 23:59:59");
			PagingEntity pe = new PagingEntity("News", "PublishDate desc");
			pe.PageSize = Pagination.PageSize;
			pe.PageIndex = Pagination.PageIndex;
			pe.WhereClause = "(cdate(PublishDate)>=cdate('" + startDate + "')) and (cdate(PublishDate)<=cdate('" + endDate + "'))";
			DataTable table = YOCOSBusiness.NewsManager.GetNewsListPagin(pe);
			this.rptNewsList.DataSource = table;
			this.rptNewsList.DataBind();
			this.Pagination.BindName = "BindData";
			this.Pagination.Count = pe.RecordCount;
			this.Pagination.BindPager();
		}
	}
}